#ifndef CAPI20_H_
#define CAPI20_H_

#define TRUE 1
#define FALSE 0
#define byte unsigned char
#define word unsigned short
#define dword unsigned long
#define MIN(a,b) ((a)>(b) ? (b) : (a))
#define MAX(a,b) ((a)>(b) ? (a) : (b))

#if defined(WINNT_APPL)
#define far
#define _pascal
#endif

        /* operations on message queues                             */

#if defined(DOS) || defined(VXD)
#define CAPI20_COMMAND          0x1400
#define _CAPI_REGISTER          0x1401
#define _CAPI_RELEASE           0x1402
#define _CAPI_PUT               0x1403
#define _CAPI_GET               0x1404
#define _CAPI_SET_SIGNAL        0x1405
#define _CAPI_DEINSTALL         0x1406

#define _CAPI_GET_MAN           0x14f0
#define _CAPI_GET_VERSION       0x14f1
#define _CAPI_GET_SERIAL        0x14f2
#define _CAPI_GET_PROFILE       0x14f3
#endif

#if defined(WINNT)

        /* the common device type for CAPI20 drivers */
#define FILE_DEVICE_CAPI20 0x8001

        /* DEVICE_CONTROL codes for user and kernel mode applications */
#define CAPI20_CTL_REGISTER             0x0801
#define CAPI20_CTL_RELEASE              0x0802
#define CAPI20_CTL_GET_MANUFACTURER     0x0805
#define CAPI20_CTL_GET_VERSION          0x0806
#define CAPI20_CTL_GET_SERIAL           0x0807
#define CAPI20_CTL_GET_PROFILE          0x0808

        /* INTERNAL_DEVICE_CONTROL codes for kernel mode applicatios only */

#define CAPI20_CTL_PUT_MESSAGE          0x0803
#define CAPI20_CTL_GET_MESSAGE          0x0804


        /* the wrapped codes as required by the system */

#define CAPI_CTL_CODE(f,m) CTL_CODE(FILE_DEVICE_CAPI20,f,m,FILE_ANY_ACCESS)

#define IOCTL_CAPI_REGISTER             CAPI_CTL_CODE(CAPI20_CTL_REGISTER,METHOD_BUFFERED)
#define IOCTL_CAPI_RELEASE              CAPI_CTL_CODE(CAPI20_CTL_RELEASE,METHOD_BUFFERED)
#define IOCTL_CAPI_GET_MANUFACTURER     CAPI_CTL_CODE(CAPI20_CTL_GET_MANUFACTURER,METHOD_BUFFERED)
#define IOCTL_CAPI_GET_VERSION          CAPI_CTL_CODE(CAPI20_CTL_GET_VERSION,METHOD_BUFFERED)
#define IOCTL_CAPI_GET_SERIAL           CAPI_CTL_CODE(CAPI20_CTL_GET_SERIAL,METHOD_BUFFERED)
#define IOCTL_CAPI_GET_PROFILE          CAPI_CTL_CODE(CAPI20_CTL_GET_PROFILE,METHOD_BUFFERED)

#define IOCTL_CAPI_PUT_MESSAGE          CAPI_CTL_CODE(CAPI20_CTL_PUT_MESSAGE,METHOD_BUFFERED)
#define IOCTL_CAPI_GET_MESSAGE          CAPI_CTL_CODE(CAPI20_CTL_GET_MESSAGE,METHOD_BUFFERED)

struct capi_register_params {
  word MessageBufferSize;
  word maxLogicalConnection;
  word maxBDataBlocks;
  word maxBDataLen;
};
#endif

typedef struct api_profile_s {
  word          Number;
  word          Channels;
  dword         Global_Options;
  dword         B1_Protocols;
  dword         B2_Protocols;
  dword         B3_Protocols;
} API_PROFILE;

        /* ISDN Common API message types                            */

#define _ALERT_R                        0x8001
#define _CONNECT_R                      0x8002
#define _CONNECT_I                      0x8202
#define _CONNECT_ACTIVE_I               0x8203
#define _DISCONNECT_R                   0x8004
#define _DISCONNECT_I                   0x8204
#define _LISTEN_R                       0x8005
#define _INFO_R                         0x8008
#define _INFO_I                         0x8208

#define _SELECT_B_REQ                   0x8041

#define _FACILITY_R                     0x8080
#define _FACILITY_I                     0x8280
#define _CONNECT_B3_R                   0x8082
#define _CONNECT_B3_I                   0x8282
#define _CONNECT_B3_ACTIVE_I            0x8283
#define _DISCONNECT_B3_R                0x8084
#define _DISCONNECT_B3_I                0x8284
#define _DATA_B3_R                      0x8086
#define _DATA_B3_I                      0x8286
#define _RESET_B3_R                     0x8087
#define _RESET_B3_I                     0x8287
#define _CONNECT_B3_T90_ACTIVE_I        0x8288

#define _MANUFACTURER_R                 0x80ff
#define _MANUFACTURER_I                 0x82ff

        /* OR this to convert a REQUEST to a CONFIRM                */

#define CONFIRM                 0x0100

        /* OR this to convert a INDICATION to a RESPONSE            */

#define RESPONSE                0x0100

/*------------------------------------------------------------------*/
/* diehl isdn private MANUFACTURER codes                            */
/*------------------------------------------------------------------*/

#define _DI_MANU_ID             0x44444944

#define _DI_ASSIGN_PLCI         0x01

/*------------------------------------------------------------------*/
/* parameter structures                                             */
/*------------------------------------------------------------------*/

        /* ALERT-REQUEST                                            */

typedef struct {
  byte structs[1];      /* Additional Info */
} _ALT_REQP;

        /* ALERT-CONFIRM                                            */

typedef struct {
  word Info;
} _ALT_CONP;

        /* CONNECT-REQUEST                                          */

typedef struct {
  word CIP_Value;
  byte structs[1];      /* Called party number,
                           Called party subaddress,
                           Calling party number,
                           Calling party subaddress,
                           B_protocol,
                           BC,
                           LLC,
                           HLC,
                           Additional Info */
} _CON_REQP;

        /* CONNECT-CONFIRM                                          */

typedef struct {
  word Info;
} _CON_CONP;

        /* CONNECT-INDICATION                                       */

typedef struct {
  word CIP_Value;
  byte structs[1];      /* Called party number,
                           Called party subaddress,
                           Calling party number,
                           Calling party subaddress,
                           BC,
                           LLC,
                           HLC,
                           Additional Info */
} _CON_INDP;

        /* CONNECT-RESPONSE                                         */

typedef struct {
  word Accept;
  byte structs[1];      /* B_protocol,
                           Connected party number,
                           Connected party subaddress,
                           LLC */
} _CON_RESP;

        /* CONNECT-ACTIVE-INDICATION                                */

typedef struct {
  byte structs[1];      /* Connected party number,
                           Connected party subaddress,
                           LLC */
} _CON_A_INDP;

        /* CONNECT-ACTIVE-RESPONSE                                  */

typedef struct {
  byte structs[1];      /* empty */
} _CON_A_RESP;

        /* DISCONNECT-REQUEST                                       */

typedef struct {
  byte structs[1];      /* Additional Info */
} _DIS_REQP;

        /* DISCONNECT-CONFIRM                                       */

typedef struct {
  word Info;
} _DIS_CONP;

        /* DISCONNECT-INDICATION                                    */

typedef struct {
  word Info;
} _DIS_INDP;

        /* DISCONNECT-RESPONSE                                      */

typedef struct {
  byte structs[1];      /* empty */
} _DIS_RESP;

        /* LISTEN-REQUEST                                           */

typedef struct {
  dword Info_Mask;
  dword CIP_Mask;
  byte structs[1];      /* Calling party number,
                           Calling party subaddress */
} _LIS_REQP;

        /* LISTEN-CONFIRM                                           */

typedef struct {
  word Info;
} _LIS_CONP;

        /* INFO-REQUEST                                             */

typedef struct {
  byte structs[1];      /* Called party number,
                           Additional Info */
} _INF_REQP;

        /* INFO-CONFIRM                                             */

typedef struct {
  word Info;
} _INF_CONP;

        /* INFO-INDICATION                                          */

typedef struct {
  word Number;
  byte structs[1];      /* Info element */
} _INF_INDP;

        /* INFO-RESPONSE                                            */

typedef struct {
  byte structs[1];      /* empty */
} _INF_RESP;

        /* SELECT-B-REQUEST                                         */

typedef struct {
  byte structs[1];      /* B-protocol */
} _SEL_B_REQP;

        /* SELECT-B-CONFIRM                                         */

typedef struct {
  word Info;
} _SEL_B_CONP;

        /* FACILITY-REQUEST */

typedef struct {
  word Selector;
  byte structs[1];      /* Facility parameters */
} _FAC_REQP;

        /* FACILITY-CONFIRM */

typedef struct {
  word Info;
  word Selector;
  byte structs[1];      /* Facility parameters */
} _FAC_CONP;

        /* FACILITY-INDICATION */

typedef struct {
  word Selector;
  byte structs[1];      /* Facility parameters */
} _FAC_INDP;

        /* FACILITY-RESPONSE */

typedef struct {
  word Selector;
  byte structs[1];      /* Facility parameters */
} _FAC_RESP;

        /* CONNECT-B3-REQUEST                                       */

typedef struct {
  byte structs[1];      /* NCPI */
} _CON_B3_REQP;

        /* CONNECT-B3-CONFIRM                                       */

typedef struct {
  word Info;
} _CON_B3_CONP;

        /* CONNECT-B3-INDICATION                                    */

typedef struct {
  byte structs[1];      /* NCPI */
} _CON_B3_INDP;

        /* CONNECT-B3-RESPONSE                                      */

typedef struct {
  word Accept;
  byte structs[1];      /* NCPI */
} _CON_B3_RESP;

        /* CONNECT-B3-ACTIVE-INDICATION                             */

typedef struct {
  byte structs[1];      /* NCPI */
} _CON_B3_A_INDP;

        /* CONNECT-B3-ACTIVE-RESPONSE                               */

typedef struct {
  byte structs[1];      /* empty */
} _CON_B3_A_RESP;


        /* DISCONNECT-B3-REQUEST                                    */

typedef struct {
  byte structs[1];      /* NCPI */
} _DIS_B3_REQP;

        /* DISCONNECT-B3-CONFIRM                                    */

typedef struct {
  word Info;
} _DIS_B3_CONP;

        /* DISCONNECT-B3-INDICATION                                 */

typedef struct {
  word Info;
  byte structs[1];      /* NCPI */
} _DIS_B3_INDP;

        /* DISCONNECT-B3-RESPONSE                                   */

typedef struct {
  byte structs[1];      /* empty */
} _DIS_B3_RESP;

        /* DATA-B3-REQUEST                                          */

typedef struct {
  dword         Data;
  word          Data_Length;
  word          Number;
  word          Flags;
} _DAT_B3_REQP;

        /* DATA-B3-CONFIRM                                          */

typedef struct {
  word          Number;
  word          Info;
} _DAT_B3_CONP;

        /* DATA-B3-INDICATION                                       */

typedef struct {
  dword         Data;
  word          Data_Length;
  word          Number;
  word          Flags;
} _DAT_B3_INDP;

        /* DATA-B3-RESPONSE                                         */

typedef struct {
  word          Number;
} _DAT_B3_RESP;

        /* RESET-B3-REQUEST                                         */

typedef struct {
  byte structs[1];      /* NCPI */
} _RES_B3_REQP;

        /* RESET-B3-CONFIRM                                         */

typedef struct {
  word Info;
} _RES_B3_CONP;

        /* RESET-B3-INDICATION                                      */

typedef struct {
  byte structs[1];      /* NCPI */
} _RES_B3_INDP;

        /* RESET-B3-RESPONSE                                        */

typedef struct {
  byte structs[1];      /* empty */
} _RES_B3_RESP;

        /* CONNECT-B3-T90-ACTIVE-INDICATION                         */
typedef struct {
  byte structs[1];      /* NCPI */
} _CON_B3_T90_A_INDP;

        /* CONNECT-B3-T90-ACTIVE-RESPONSE                           */
typedef struct {
  word Reject;
  byte structs[1];      /* NCPI */
} _CON_B3_T90_A_RESP;

/*------------------------------------------------------------------*/
/* message structure                                                */
/*------------------------------------------------------------------*/

typedef struct _API_MSG CAPI_MSG;
typedef struct _MSG_HEADER CAPI_MSG_HEADER;

struct _API_MSG {
  struct _MSG_HEADER {
    word        length;
    word        appl_id;
    word        command;
    word        number;
    byte        controller;
    byte        plci;
    word        ncci;
  } header;
  union {
    _ALT_REQP           alert_req;
    _ALT_CONP           alert_con;
    _CON_REQP           connect_req;
    _CON_CONP           connect_con;
    _CON_INDP           connect_ind;
    _CON_RESP           connect_res;
    _CON_A_INDP         connect_a_ind;
    _CON_A_RESP         connect_a_res;
    _DIS_REQP           disconnect_req;
    _DIS_CONP           disconnect_con;
    _DIS_INDP           disconnect_ind;
    _DIS_RESP           disconnect_res;
    _LIS_REQP           listen_req;
    _LIS_CONP           listen_con;
    _INF_REQP           info_req;
    _INF_CONP           info_con;
    _INF_INDP           info_ind;
    _INF_RESP           info_res;
    _SEL_B_REQP         select_b_req;
    _SEL_B_CONP         select_b_con;
    _FAC_REQP           facility_req;
    _FAC_CONP           facility_con;
    _FAC_INDP           facility_ind;
    _FAC_RESP           facility_res;
    _CON_B3_REQP        connect_b3_req;
    _CON_B3_CONP        connect_b3_con;
    _CON_B3_INDP        connect_b3_ind;
    _CON_B3_RESP        connect_b3_res;
    _CON_B3_A_INDP      connect_b3_a_ind;
    _CON_B3_A_RESP      connect_b3_a_res;
    _DIS_B3_REQP        disconnect_b3_req;
    _DIS_B3_CONP        disconnect_b3_con;
    _DIS_B3_INDP        disconnect_b3_ind;
    _DIS_B3_RESP        disconnect_b3_res;
    _DAT_B3_REQP        data_b3_req;
    _DAT_B3_CONP        data_b3_con;
    _DAT_B3_INDP        data_b3_ind;
    _DAT_B3_RESP        data_b3_res;
    _RES_B3_REQP        reset_b3_req;
    _RES_B3_CONP        reset_b3_con;
    _RES_B3_INDP        reset_b3_ind;
    _RES_B3_RESP        reset_b3_res;
    _CON_B3_T90_A_INDP  connect_b3_t90_a_ind;
    _CON_B3_T90_A_RESP  connect_b3_t90_a_res;
    byte                b[200];
  } info;
};

/*------------------------------------------------------------------*/
/* non-fatal errors                                                 */
/*------------------------------------------------------------------*/

#define _NCPI_IGNORED           0x0001
#define _FLAGS_IGNORED          0x0002
#define _ALERT_IGNORED          0x0003

/*------------------------------------------------------------------*/
/* API function error codes                                         */
/*------------------------------------------------------------------*/

#define GOOD                            0x0000
#define _TOO_MANY_APPLICATIONS          0x1001
#define _BLOCK_TOO_SMALL                0x1002
#define _BUFFER_TOO_BIG                 0x1003
#define _MSG_BUFFER_TOO_SMALL           0x1004
#define _TOO_MUCH_CONNECTIONS           0x1005
#define _REG_RESOURCE_ERROR             0x1008
#define _REG_CAPI_NOT_INSTALLED         0x1009

#define _WRONG_APPL_ID                  0x1101
#define _BAD_MSG                        0x1102
#define _QUEUE_FULL                     0x1103
#define _GET_NO_MSG                     0x1104
#define _MSG_LOST                       0x1105
#define _WRONG_NOTIFY                   0x1106
#define _CAPI_BUSY                      0x1107
#define _RESOURCE_ERROR                 0x1108
#define _CAPI_NOT_INSTALLED             0x1109
#define _NO_EXTERNAL_EQUIPMENT          0x110a
#define _ONLY_EXTERNAL_EQUIPMENT        0x110b

/*------------------------------------------------------------------*/
/* addressing/coding error codes                                    */
/*------------------------------------------------------------------*/

#define _WRONG_STATE                    0x2001
#define _WRONG_IDENTIFIER               0x2002
#define _OUT_OF_PLCI                    0x2003
#define _OUT_OF_NCCI                    0x2004
#define _OUT_OF_LISTEN                  0x2005
#define _OUT_OF_FAX                     0x2006
#define _WRONG_MESSAGE_FORMAT           0x2007

/*------------------------------------------------------------------*/
/* configuration error codes                                        */
/*------------------------------------------------------------------*/

#define _B1_NOT_SUPPORTED               0x3001
#define _B2_NOT_SUPPORTED               0x3002
#define _B3_NOT_SUPPORTED               0x3003
#define _B1_PARM_NOT_SUPPORTED          0x3004
#define _B2_PARM_NOT_SUPPORTED          0x3005
#define _B3_PARM_NOT_SUPPORTED          0x3006
#define _B_STACK_NOT_SUPPORTED          0x3007
#define _NCPI_NOT_SUPPORTED             0x3008
#define _CIP_NOT_SUPPORTED              0x3009
#define _FLAGS_NOT_SUPPORTED            0x300a
#define _FACILITY_NOT_SUPPORTED         0x300b
#define _DATA_LEN_NOT_SUPPORTED         0x300c
#define _RESET_NOT_SUPPORTED            0x300d

/*------------------------------------------------------------------*/
/* reason codes                                                     */
/*------------------------------------------------------------------*/

#define _L1_ERROR                       0x3301
#define _L2_ERROR                       0x3302
#define _L3_ERROR                       0x3303
#define _OTHER_APPL_CONNECTED           0x3304
#define _L3_CAUSE                       0x3400

/*------------------------------------------------------------------*/
/* b3 reason codes                                                  */
/*------------------------------------------------------------------*/

#define _B_CHANNEL_LOST                 0x3301
#define _B2_ERROR                       0x3302
#define _B3_ERROR                       0x3303

/*------------------------------------------------------------------*/
/* fax error codes                                                  */
/*------------------------------------------------------------------*/

#define _FAX_NO_CONNECTION              0x3311
#define _FAX_TRAINING_ERROR             0x3312
#define _FAX_REMOTE_REJECT              0x3313
#define _FAX_REMOTE_ABORT               0x3314
#define _FAX_PROTOCOL_ERROR             0x3315
#define _FAX_TX_UNDERRUN                0x3316
#define _FAX_RX_OVERFLOW                0x3317
#define _FAX_LOCAL_ABORT                0x3318
#define _FAX_PARAMETER_ERROR            0x3319

/*------------------------------------------------------------------*/
/* protocol selection                                               */
/*------------------------------------------------------------------*/

#define B1_HDLC                 0
#define B1_TRANSPARENT          1
#define B1_V110_SYNC            2
#define B1_V110_ASSYNC          3
#define B1_T30                  4
#define B1_HDLC_INVERTED        5
#define B1_HDLCR                6

#define B2_X75                  0
#define B2_TRANSPARENT          1
#define B2_SDLC                 2
#define B2_LAPD                 3
#define B2_T30                  4
#define B2_PPP                  5
#define B2_TRANSPARENT_NO_CRC   6

#define B3_TRANSPARENT          0
#define B3_T90NL                1
#define B3_ISO8208              2
#define B3_X25_DCE              3
#define B3_T30                  4

/*------------------------------------------------------------------*/
/* function prototypes                                              */
/*------------------------------------------------------------------*/

#if defined(DLL)
word far pascal CAPI_REGISTER(byte far *, word, word, word, word);
word far pascal CAPI_RELEASE(word);
word far pascal CAPI_PUT_MESSAGE(word, API_MSG far *);
word far pascal CAPI_GET_MESSAGE(word, API_MSG far * far *);
word far pascal CAPI_SET_SIGNAL(word, long);
word far pascal CAPI_GET_VERSION(void);
#endif

#if defined(DOS)
#define OFAR

word CAPI_REGISTER(byte far *, word, word, word, word);
word CAPI_RELEASE(word);
word CAPI_SET_SIGNAL(word,dword);
word CAPI_PUT_MESSAGE(word, CAPI_MSG far *);
word CAPI_GET_MESSAGE(word, CAPI_MSG far * *);
word CAPI_GET_VERSION(void);
word CAPI_GET_PROFILE(word, API_PROFILE far *);

void _cdecl capi_sendf(word, word, dword, word, byte *,...);
#endif

#if defined(WINNT_APPL)
word CAPI_REGISTER( void *, word, word, word, word);
word CAPI_RELEASE(word);
word CAPI_SET_SIGNAL(word, long);
word CAPI_PUT_MESSAGE(word, CAPI_MSG *);
word CAPI_GET_MESSAGE(word, CAPI_MSG * *);
word CAPI_GET_VERSION( void );
word CAPI_GET_PROFILE(word, API_PROFILE far *);
word CAPI_WAIT_MESSAGE(word, long);

void _cdecl capi_sendf(word, word, dword, word, byte *,...);
#endif

#endif
